package home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;

import net.ck.hzq.R;

public class TopFixedAdapter extends DelegateAdapter.Adapter<TopFixedAdapter.TopFixDividerVH> {

    private final Context mContext;
    private boolean mShow;

    public TopFixedAdapter(Context context) {
        mContext = context;
    }

    public void show(boolean show) {
        mShow = show;
        notifyDataSetChanged();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new SingleLayoutHelper();
    }

    @NonNull
    @Override
    public TopFixDividerVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new TopFixDividerVH(LayoutInflater.from(mContext).inflate(R.layout.item_home_top_banner, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull TopFixDividerVH holder, int i) {
        holder.infor.setOnClickListener(view -> {
            Toast.makeText(mContext, "跳转啦", Toast.LENGTH_SHORT).show();
        });
        holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 获得焦点
                } else {
                    // 失去焦点
                }
            }
        });
        holder.editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String str = holder.editText.getText().toString().trim();
                    //关闭软键盘
                    hideKeyboard(v);
                    //do something
                    //doSearch();
                    Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mShow ? 1 : 0;
    }

    class TopFixDividerVH extends RecyclerView.ViewHolder {
        private ImageView infor;
        private EditText editText;

        TopFixDividerVH(@NonNull View itemView) {
            super(itemView);
            editText = itemView.findViewById(R.id.top_ss);
            infor = itemView.findViewById(R.id.top_questonimg);
        }
    }

    /**
     * 隐藏软键盘
     *
     * @param context :上下文环境，一般为Activity实例
     * @param view    :一般为EditText
     */
    public static void hideKeyboard(View view) {
        InputMethodManager manager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

}
